<?php
include('config.inc.php');
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$charset\">";

function getImageName(){
	Global $year,$sum,$sumDB;
	$month = date("m");
	$day = date("d");
	if(checkYear()){
		//ยังเป็นปีปัจจุบันอยู่ ดึงค่าแล้วบวกเพิ่มอีก 1 จัดรูปแบบให้อยู่ ในเลข 5 หลัก
		$sumDB++;
		//echo $sum++."<br>";
		//echo $sum."<br>";
		if($sumDB < 10){
			$sum = "0000".$sumDB;
		}else if($sumDB < 100){
			$sum = "000".$sumDB;
		}else if($sumDB < 1000){
			$sum = "00".$sumDB;
		}else{
			$sum = $sumDB;
		}
		//echo $sum."<br>";
		$detail_no = $year."-".$sum;
		return $detail_no;
	}
}


function getDetailNo(){
	Global $year,$sum,$sumDB;
	$month = date("m");
	$day = date("d");
	if(checkYear()){
		//ยังเป็นปีปัจจุบันอยู่ ดึงค่าแล้วบวกเพิ่มอีก 1 จัดรูปแบบให้อยู่ ในเลข 5 หลัก
		$sumDB++;
		//echo $sum++."<br>";
		//echo $sum."<br>";
		if($sumDB < 10){
			$sum = "0000".$sumDB;
		}else if($sumDB < 100){
			$sum = "000".$sumDB;
		}else if($sumDB < 1000){
			$sum = "00".$sumDB;
		}else{
			$sum = $sumDB;
		}
		//echo $sum."<br>";
		$detail_no = $year."-".$month."-".$day."/".$sum;
		return $detail_no;
	}
}
function getDbNo(){
	Global $year,$sum,$sumDB;
	$month = date("m");
	$day = date("d");
	if(checkYear()){
		//ยังเป็นปีปัจจุบันอยู่ ดึงค่าแล้วบวกเพิ่มอีก 1 จัดรูปแบบให้อยู่ ในเลข 5 หลัก
		$sumDB;
		//echo $sum++."<br>";
		//echo $sum."<br>";
		if($sumDB < 10){
			$sum = "0000".$sumDB;
		}else if($sumDB < 100){
			$sum = "000".$sumDB;
		}else if($sumDB < 1000){
			$sum = "00".$sumDB;
		}else{
			$sum = $sumDB;
		}
		//echo $sum."<br>";
		$detail_no = $year."-".$month."-".$day."/".$sum;
		return $detail_no;
	}
}
function checkYear(){
	Global $detail_no_format,$sumDB,$year;
	$year_en = date("Y");
	$year_th = date("Y")+543;
	if($detail_no_format == 1){
		$year = $year_en;
	}else if($detail_no_format == 2){
		$year = $year_th;
	}
	$sql = "select * from sumfault order by id desc";
	$result = mysql_query($sql);
	$num = @mysql_num_rows($result);
	$arr = @mysql_fetch_array($result);
	if(!$num){
		$yearadd = "insert into sumfault value('', '$year_th', '0', '$year_en')";
		$add_result = mysql_query($yearadd)or die('Error : ไม่สามารถกำหนดค่าเริ่มต้น ตาราง sumfault');
		if($add_result){
			return true;
		}else{
			return false;
		}
	}else if($num != 0){
		//ตรวจสอบการดึงค่าปีในฐานข้อมูลมาเทียบกับปัจจุบัน
		if($detail_no_format == 1){
			$yearDB = $arr['year'];
		}else if($detail_no_format == 2){
			$yearDB = $arr['year_thai'];
		}
		$sumDB = $arr['sum'];
		//ตรวจว่าีปีในฐานข้อมูลยังเป็นปีปัจจับันหรือไม่
		if($year == $yearDB){
			return true;
		}else{
			// ไม่ใช่ เพิ่มปีปัจจุบันในฐานข้อมูล
			$yearadd = "insert into sumfault value('', '$year_th', '0', '$year_en')";
			$add_result = mysql_query($yearadd)or die('Error : ไม่สามารถกำหนดค่าของปีปัจจุบันในตาราง sumfault');
			if($add_result){
				return true;
			}else{
				return false;
			}
		}

	}
}
function sendMailFault($no){
	global $Server,$Dir,$sys_mail,$email;
	//setting mailserver
ini_set("SMTP","smtp.ttt.co.th");
ini_set("smtp_port","25");
ini_set("sendmail_from","fcr_systems@ttt.co.th");

	$sql = "SELECT *,u.username as un_user, u.name as un_name,u.pnthai as un_pnthai, u.unit_code as un_unit_code,u.tel as un_tel,u.fax as un_fax,u.office as un_office
	FROM send se  
inner join detail d on se.Fault_type = d.fault_type 
inner join location l on d.location_id = l.location_id
inner join users u on u.username = se.Email
inner join users u2 on u2.username =d.username_note 
inner join fault_type ft on ft.id = d.fault_type
inner join status_type st on st.status_id = d.status
where d.no='$no'";
	$result = mysql_query($sql);
	$num = mysql_num_rows($result);
	$arr = mysql_fetch_array($result);

	/*
	 * code sendmail
	 */
	$e_mail = $arr['Email'].$email;
	$to  = $e_mail;
	// subject
	$subject = 'แจ้งเหตุเสียอุปกรณ์สำนักงาน';
	$subject ="=?UTF-8?B?".base64_encode($subject)."?=";
	// message
	$message = "
	<html>
	<head>
	  <title>ระบบแจ้งเหตุเสีย</title>
	</head>
	<body>
<table width=\"889\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
  <tr>
    <td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\">
        <tr>
          <td width=\"12%\" style=\"vertical-align: top;padding:5px 0px 5px 10px;\"><img src=\"http://".$Server.$Dir."images/logo_ttt.jpg\" width=100 height=100></td>
          <td width=\"59%\" valign=\"center\"><h2 align=\"center\">ใบแจ้งเหตุเสียอุปกรณ์สำนักงาน</h2></td>
          <td width=\"29%\" valign=\"bottom\" style=\"vertical-align: bottom;padding:0px 10px 0px 0px;\"><br>
              <div align=\"right\">
                <p><small>เลขที่&nbsp;
                      ".$no."
                </small></p>
                <p><small>".thDate($arr['day_in'])."&nbsp;น.</small></p>
            </div></td>
        </tr>
        <tr>
          <td colspan=\"3\"><hr width=\"98%\">
                        </td>
        </tr>
        <tr>
          <td colspan=\"3\"><div style=\"vertical-align: top;padding:10px 0px 20px 60px;\"><strong><small>รายละเอียดการแจ้งเสีย</small></strong></div>
              <table width=\"95%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" >
                <tr>
                  <td width=\"19%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>เลขที่แจ้งเสีย</small></strong></td>
                  <td width=\"14%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>ประเภทเหตุเสีย</small></strong></td>
                  <td width=\"22%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>สถานที่</small></strong></td>
                  <td width=\"24%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>เหตุเสีย</small></strong></td>
                  <td width=\"21%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>สถานะการแจ้ง</small></strong></td>
                </tr>
                <tr>
                  <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>".$arr['no']."</small></td>
                  <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>".$arr['fault_name']."</small></td>
                  <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>".$arr['location_id']."-".$arr['site_name']."</small></td>
                  <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>".$arr['problem']."</small></td>
                  <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>".$arr['status_detail']."</small></td>
                </tr>
                <tr>
                  <td colspan=\"5\"><hr width=\"80%\">
                      <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >
                        <tr>
                          <td colspan=\"2\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><strong><small>ผู้แจ้งเหตุเสีย</small></strong><small> : ".$arr['un_name']."</small></td>
                          <td width=\"25%\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>โทรศัพท์</strong> : ".$arr['un_tel']."</small></td>
                          <td width=\"20%\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><strong><small>โทรสาร</small></strong><small> : ".$arr['un_fax']."</small></td>
                          <td width=\"27%\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>อีเมลล์</strong> : ".$arr['un_user']."@ttt.co.th"."</small></td>
                        </tr>
                        <tr>
                          <td colspan=\"2\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>หน่วยงาน</strong> : ".$arr['un_office']."</small></td>
                          <td style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>จังหวัด</strong> : ".$arr['un_pnthai']."</small></td>
                          <td style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>unit_Code</strong> : ".$arr['un_unit_code']."</small>&nbsp;</td>
                          <td style=\"vertical-align: top;padding:10px 0px 10px 20px;\">&nbsp;</td>
                        </tr>
                    </table></td>
                </tr>
              </table>
              <hr width=\"98%\">
              <div align='center'><small><a href=\"http://".$Server.$Dir."showfaultdetail.php?no=".$no."&process=".$arr['Email']."&ac=pro\">คลิกดูรายงาน</a></small></div>
		  </td>
        </tr>
    </table></td>
  </tr>
</table>
	  
	</body>
	</html>
	";
	
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	
	// Additional headers
		$headers .= "To: คุณ ".$arr['senduser']." <".$to.">". "\r\n";
		$headers .= "From: FCR Systems <".$sys_mail.">" . "\r\n";
		//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
		//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
	//echo $message;
	// Mail it
	if(@mail($to, $subject, $message, $headers)){
			return true;
		}else{
			return false;
		}
		//return $MSG;

}
function thDate($enDate){
	if($enDate == ""){
		return false;
		exit();
	}
	$str = explode(" ",$enDate);
	if(strlen($str[0])==1){
		$thday = $str[0];
		$Date = $str[1];
		$time = $str[2];
		$thDayArray = array("อาทิตย์", "จันทร์", "อังคาร", "พุธ", "พฤหัสบดี", "ศุกร์", "เสาร์");
		$thMonthArray = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤษจิกายน","ธันวาคม");
		$thday = $thDayArray[$thday];
		$day = substr($Date,8,2);
		if($day < 10){
		 $day = substr($day,1,1);
		}
		$month = substr($Date,5,2);
		$year = substr($Date,0,4);
		$thyear = $year+543;
		$thmonth = $thMonthArray[$month-1];
		$thDateNew = "วัน".$thday."ที่ ".$day." ".$thmonth." ".$thyear." ".$time;
		return $thDateNew;
	}else{
		$Date = $str[0];
		$time = $str[1];
		$thDayArray = array("อาทิตย์", "จันทร์", "อังคาร", "พุธ", "พฤหัสบดี", "ศุกร์", "เสาร์");
		$thMonthArray = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤษจิกายน","ธันวาคม");
		$thday = $thDayArray[$thday];
		$day = substr($Date,8,2);
		if($day < 10){
		 $day = substr($day,1,1);
		}
		$month = substr($Date,5,2);
		$year = substr($Date,0,4);
		$thyear = $year+543;
		$thmonth = $thMonthArray[$month-1];
		$thDateNew = $day." ".$thmonth." ".$thyear." ".$time;
		return $thDateNew;
		
	}
}
function enDate($thDate){
	if($thDate == ""){
		return false;
		exit();
	}
	$str = explode(" ",$thDate);
	if(strlen($str[0])==1){
		$enday = $str[0];
		$Date = $str[1];
		$time = $str[2];
		$enDayArray = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		$enMonthArray = array("Januery","Febluery","March","April","May","June","July","August","September","October","November","December");
		$enday = $enDayArray[$enday];
		$day = substr($Date,8,2);
		if($day < 10){
		 $day = substr($day,1,1);
		}
		$month = substr($Date,5,2);
		$year = substr($Date,0,4);
		$enmonth = $enMonthArray[$month-1];
		$enDateNew = $enday." ".$day." ".$enmonth." ".$year." ".$time;
		return $enDateNew;
	}else{
		$Date = $str[0];
		$time = $str[1];
		$enDayArray = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
		$enMonthArray = array("Januery","Febluery","March","April","May","June","July","August","September","October","November","December");
		$enday = $enDayArray[$enday];
		$day = substr($Date,8,2);
		if($day < 10){
		 $day = substr($day,1,1);
		}
		$month = substr($Date,5,2);
		$year = substr($Date,0,4);
		$enmonth = $enMonthArray[$month-1];
		$enDateNew = $day." ".$enmonth." ".$year." ".$time;
		return $enDateNew;
	}
}
function getAppNo(){
	Global $year,$sumApp,$sumDbApp;
	$month = date("m");
	$day = date("d");
	if(checkYearApp()){
		//ยังเป็นปีปัจจุบันอยู่ ดึงค่าแล้วบวกเพิ่มอีก 1 จัดรูปแบบให้อยู่ ในเลข 5 หลัก
		$sumDbApp++;
		//echo $sum++."<br>";
		//echo $sum."<br>";
		if($sumDbApp < 10){
			$sumApp = "0000".$sumDbApp;
		}else if($sumDbApp < 100){
			$sumApp = "000".$sumDbApp;
		}else if($sumDbApp < 1000){
			$sumApp = "00".$sumDbApp;
		}else{
			$sumApp = $sumDbApp;
		}
		//echo $sum."<br>";
		$App_no = $year."-".$month."-".$day."/".$sumApp;
		return $App_no;
	}
}
function getDbAppNo(){
	Global $year,$sumApp,$sumDbApp;
	$month = date("m");
	$day = date("d");
	if(checkYearApp()){
		//ยังเป็นปีปัจจุบันอยู่ ดึงค่าแล้วบวกเพิ่มอีก 1 จัดรูปแบบให้อยู่ ในเลข 5 หลัก
		$sumDbApp;
		//echo $sum++."<br>";
		//echo $sum."<br>";
		if($sumDbApp < 10){
			$sumApp = "0000".$sumDbApp;
		}else if($sumDbApp < 100){
			$sumApp = "000".$sumDbApp;
		}else if($sumDbApp < 1000){
			$sumApp = "00".$sumDbApp;
		}else{
			$sumApp = $sumDbApp;
		}
		//echo $sum."<br>";
		$App_no = $year."-".$month."-".$day."/".$sumApp;
		return $App_no;
	}
}
function checkYearApp(){
	Global $approve_no_format,$sumDbApp,$year;
	$year_en = date("Y");
	$year_th = date("Y")+543;
	if($approve_no_format == 1){
		$year = $year_en;
	}else if($approve_no_format == 2){
		$year = $year_th;
	}
	$sql = "select * from sumapp order by id desc";
	$result = mysql_query($sql);
	$num = @mysql_num_rows($result);
	$arr = @mysql_fetch_array($result);
	if(!$num){
		$yearadd = "insert into sumapp value('', '$year_en', '$year_th', '0')";
		$add_result = mysql_query($yearadd)or die('Error : ไม่สามารถกำหนดค่าเริ่มต้น ตาราง sumapp');
		if($add_result){
			return true;
		}else{
			return false;
		}
	}else if($num != 0){
		//ตรวจสอบการดึงค่าปีในฐานข้อมูลมาเทียบกับปัจจุบัน
		if($approve_no_format == 1){
			$yearDB = $arr['app_year'];
		}else if($approve_no_format == 2){
			$yearDB = $arr['app_year_thai'];
		}
		$sumDbApp = $arr['app_sum'];
		//ตรวจว่าีปีในฐานข้อมูลยังเป็นปีปัจจับันหรือไม่
		if($year == $yearDB){
			return true;
		}else{
			// ไม่ใช่ เพิ่มปีปัจจุบันในฐานข้อมูล
			$yearadd = "insert into sumapp value('', '$year_en', '$year_th', '0')";
			$add_result = mysql_query($yearadd)or die('Error : ไม่สามารถกำหนดค่าของปีปัจจุบันในตาราง sumapp');
			if($add_result){
				return true;
			}else{
				return false;
			}
		}

	}
}
function sendMailApp($app){
	global $Server,$Dir,$email,$sys_mail;
	//setting mailserver
ini_set("SMTP","smtp.ttt.co.th");
ini_set("smtp_port","25");
ini_set("sendmail_from","fcr_systems@ttt.co.th");

	$sql = "SELECT ap.approve_no, ap.app_day, ap.fault_no,ap.app_detail, ap.app_cost, ap.app_user,
 d.day_in, ft.fault_name,lo.location_id, lo.site_name, d.problem,
un.username as un_user, un.name as un_name,un.pnthai as un_pnthai, un.unit_code as un_unit_code,un.tel as un_tel,st.status_detail,
up.username as up_user, up.name as up_name,up.pnthai as up_pnthai, up.unit_code as up_unit_code,up.tel as up_tel,
st1.status_detail as appstatus,st1.status_id as appstatusid,
ua.username as ua_user, ua.name as ua_name,ua.pnthai as ua_pnthai,ua.unit_code as ua_unit_code,ua.tel as ua_tel
FROM approve ap
left join detail d on d.no = ap.fault_no
left join fault_type ft on ft.id = d.fault_type
left join location lo on lo.location_id = d.location_id
left join users un on un.username = d.username_note
left join users up on up.username = d.username_process
left join users ua on ua.username = ap.app_user
left join status_type st on st.status_id = d.status
left join status_type st1 on st1.status_id = ap.app_status
where ap.approve_no = '$app'";
	@$result = mysql_query($sql);
	@$num = mysql_num_rows($result);
	$arr = mysql_fetch_array($result);

	/*
	 * code sendmail
	 */
	$e_mail = $arr['app_user'].$email;
	$to  = $e_mail;
	// subject
	$subject = 'ขออนุมัติการดำเนินการแก้ไปเหตุเสีย';
	$subject ="=?UTF-8?B?".base64_encode($subject)."?=";
	// message
	$message = "
	<html>
	<head>
	  <title>ระบบแจ้งเหตุเสีย</title>
	</head>
	<body>
<table width=\"889\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
    <tr>
      <td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\">
        <tr>
          <td width=\"12%\" style=\"vertical-align: top;padding:5px 0px 5px 10px;\"><img src=\"http://".$Server.$Dir."images/logo_ttt.jpg\" width=100 height=100></td>
        <td width=\"59%\" valign=\"center\"><small>
          <h2 align=\"center\">ใบขออนุมัติการแจ้งซ่อมอุปกรณ์และสำนักงาน</h2></small></td>
          <td width=\"29%\" valign=\"bottom\" style=\"vertical-align: bottom;padding:0px 10px 0px 0px;\"><br>
            <div align=\"right\"><small><p>เลขที่&nbsp;
			        ".$arr['approve_no']."
			      </p>
			    <p>".thDate($arr['app_day'])."&nbsp;น.</p>
			    </small>
		  </div></td></tr>
        <tr>
          <td colspan=\"3\"><hr width=\"98%\"><br></td>
        </tr>
        <tr>
          <td colspan=\"3\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr>
              <td width=\"28%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\" ><small><strong>ผู้ขออนุมัติ</strong> : ". $arr['up_name']."</small></td>
			  <td width=\"24%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>โทรศัพท์</strong> : ". $arr['up_tel']."</small></td>
              <td width=\"19%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>โทรสาร</strong> : ". $arr['up_fax']."</small></td>
              <td width=\"29%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>อีเมลล์</strong> : ". $arr['up_user']."@ttt.co.th"."</small></td>
            </tr>
            <tr>
              <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>หน่วยงาน</strong> : ". $arr['up_office']."</small></td>
              <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>จังหวัด</strong> : ". $arr['up_pnthai']."</small></td>
              <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small>&nbsp;</small></td>
              <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small>&nbsp;</small></td>
            </tr>
            <tr>
              <td colspan=\"4\" ><br>
                <hr width=\"98%\"></td>
              </tr>
          </table>            
            			<div style=\"vertical-align: top;padding:10px 0px 20px 60px;\"><strong><small>รายละเอียดการแจ้งเสีย</small></strong></div>
            <table width=\"95%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" >
              <tr>
                <td width=\"19%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>เลขที่แจ้งเสีย</small></strong></td>
                <td width=\"14%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>ประเภทเหตุเสีย</small></strong></td>
              <td width=\"22%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>สถานที่</small></strong></td>
                <td width=\"24%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>เหตุเสีย</small></strong></td>
                <td width=\"21%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong><small>สถานะการแจ้ง</small></strong></td>
              </tr>
              <tr>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>". $arr['fault_no']."</small></td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>". $arr['fault_name']."</small></td>
              <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>". $arr['location_id']."-".$arr['site_name']."</small></td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>". $arr['problem']."</small></td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><small>". $arr['status_detail']."</small></td>
              </tr>
			  <tr>
			    <td colspan=\"5\">
			      <hr width=\"80%\">
			      <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >
			  <tr>
                <td colspan=\"2\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><strong><small>ผู้แจ้งเหตุเสีย</small></strong><small> : ". $arr['un_name']."</small></td>
                <td width=\"25%\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>โทรศัพท์</strong> : ". $arr['un_tel']."</small></td>
                <td width=\"20%\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><strong><small>โทรสาร</small></strong><small>  : ". $arr['un_fax']."</small></td>
                <td width=\"27%\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>อีเมลล์</strong>  : ". $arr['un_user']."@ttt.co.th"."</small></td>
			    </tr>
			  <tr>
                <td colspan=\"2\" style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>หน่วยงาน</strong> : ". $arr['un_office']."</small></td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 20px;\"><small><strong>จังหวัด</strong>  : ". $arr['un_pnthai']."</small></td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 20px;\">&nbsp;</td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 20px;\">&nbsp;</td>
			    </tr>
                </table></td>
			    </tr>
            </table>
			<hr width=\"98%\">
          	<div style=\"vertical-align: top;padding:10px 0px 20px 60px;\"><strong><small>รายการขออนุมัติ</small></strong></div>
			<small>
            <table width=\"85%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
              <tr>
                <td width=\"24%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong>เลขที่ขออนุมัติ</strong></td>
              <td width=\"37%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong>รายการ</strong></td>
                <td width=\"15%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong>จำนวนเงิน</strong></td>
                <td width=\"24%\" style=\"vertical-align: top;padding:10px 0px 10px 10px;\"><strong>สถานะการอนุมัติ</strong></td>
              </tr>
              <tr>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\">". $arr['approve_no']."</td>
              <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\">". $arr['app_detail']."</td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\">". $arr['app_cost']." บาท </td>
                <td style=\"vertical-align: top;padding:10px 0px 10px 10px;\">". $arr['appstatus']."</td>
              </tr>
            </table>
            <br>
			</small>           
            <table width=\"95%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
              <tr>
                <td width=\"28%\" style=\"vertical-align: top;paddinrg:5px 0px 5px 20px;\" ><small><strong>ผู้อนุมัติ</strong> : ". $arr['ua_name']."</small></td>
                <td width=\"24%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>โทรศัพท์</strong>  : ". $arr['ua_tel']."</small></td>
                <td width=\"19%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>โทรสาร</strong>  : ". $arr['ua_fax']."</small></td>
                <td width=\"29%\" style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>อีเมลล์</strong>  : ". $arr['ua_user']."@ttt.co.th"."</small></td>
              </tr>
              <tr>
                <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>หน่วยงาน</strong>  : ". $arr['ua_office']."</small></td>
                <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small><strong>จังหวัด</strong>  : ". $arr['ua_pnthai']."</small></td>
                <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small>&nbsp;</small></td>
                <td style=\"vertical-align: top;padding:5px 0px 5px 20px;\"><small>&nbsp;</small></td>
              </tr>
              <tr>
                <td colspan=\"4\" ><br>
                                    </td>
              </tr>
            </table>
			<hr width=\"98%\">
			<br>
			<div align='center'><small><a href=\"http://".$Server.$Dir."showappdetail.php?app_no=".$arr['approve_no']."&app_u=".$arr['app_user']."&ac=app\">คลิกดูรายงาน</a></small></div>
			</td>
        </tr>
      </table>        </td>
    </tr>
  </table>
	  
	</body>
	</html>
	";
	
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	
	// Additional headers
		$headers .= "To: คุณ ".$arr['ua_name']." <".$to.">". "\r\n";
		$headers .= "From: FCR Systems <".$sys_mail.">" . "\r\n";
		//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
		//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
	//echo $message;
	// Mail it
	
	if(@mail($to, $subject, $message, $headers)){
			$MSG = "ระบบได้ส่งเมลล์เรียบร้อยแล้ว";
		}else{
			$MSG = "ระบบไม่สามารถส่งเมลล์ได้ ";
		}
		return $MSG;

}
function approveMail($appon){
	global $Server,$Dir,$email,$sys_mail;
	//setting mailserver
ini_set("SMTP","smtp.ttt.co.th");
ini_set("smtp_port","25");
ini_set("sendmail_from","fcr_systems@ttt.co.th");

	$sql = "SELECT ap.approve_no, ap.app_day, ap.fault_no,ap.app_detail, ap.app_cost, ap.app_user,
 d.day_in, ft.fault_name,lo.location_id, lo.site_name, d.problem,
un.username as un_user, un.name as un_name,un.pnthai as un_pnthai, un.unit_code as un_unit_code,un.tel as un_tel,st.status_detail,
up.username as up_user, up.name as up_name,up.pnthai as up_pnthai, up.unit_code as up_unit_code,up.tel as up_tel,
st1.status_detail as appstatus,st1.status_id as appstatusid,
ua.username as ua_user, ua.name as ua_name,ua.pnthai as ua_pnthai,ua.unit_code as ua_unit_code,ua.tel as ua_tel
FROM approve ap
left join detail d on d.no = ap.fault_no
left join fault_type ft on ft.id = d.fault_type
left join location lo on lo.location_id = d.location_id
left join users un on un.username = d.username_note
left join users up on up.username = d.username_process
left join users ua on ua.username = ap.app_user
left join status_type st on st.status_id = d.status
left join status_type st1 on st1.status_id = ap.app_status
where ap.approve_no = '$appon'";
	@$result = mysql_query($sql);
	@$num = mysql_num_rows($result);
	$arr = mysql_fetch_array($result);

	/*
	 * code sendmail
	 */
	$e_mail = $arr[up_user].$email;
	$to  = $e_mail;
	// subject
	$subject = "ผลการขออนุมัติ";
	$subject ="=?UTF-8?B?".base64_encode($subject)."?=";
	// message
	$message = "<center>ผลการขออนุมัติของคุณได้รับการพิจารณาเรียบร้อยแล้ว</center>";
	$message .= "<center><a href='http://".$Server.$Dir."showappdetail.php?app_no=".$arr['approve_no']."' >คลิกดูผลการขออนุมัติ</a></center>";
	
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	
	// Additional headers
		$headers .= "To: คุณ ".$arr['up_name']." <".$to.">". "\r\n";
		$headers .= "From: FCR Systems <".$sys_mail.">" . "\r\n";
		//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
		//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
	//echo $to.$message.$headers;
	// Mail it
	if(@mail($to, $subject, $message, $headers)){
			$MSG = "ระบบส่งอีเมล์ให้คุณ ".$arr[up_name]." เรียบร้อยแล้ว ";
		}else{
			$MSG = "ไม่สามารถส่งอีเมลล์ได้";
		}
		return $MSG;
}

/*
 * update sumfault set sum=0 where sum=100 AND year=2009
 *
 *
 */
?>